﻿using AutoMapper;
using B.S.BasicData.Domain;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interfaces;
using B.S.BasicData.Write.Api.Application.Command.W;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.S.BasicData.Write.Api.Application.CommandHandler.W
{
    /// <summary>
    /// 修改
    /// </summary>
    public class UpdatePendingShipmentWaybillsCommandHander : IRequestHandler<UpdatePendingShipmentWaybillsCommand, ApiResult<int>>
    {
        /// <summary>
        /// 货运单
        /// </summary>
        private readonly IBaseRepository<PendingShipmentWaybillsModel> _pendingShipmentWaybillsModel;
        /// <summary>
        /// automapper转换
        /// </summary>
        private readonly IMapper mapper;

        public UpdatePendingShipmentWaybillsCommandHander(IBaseRepository<PendingShipmentWaybillsModel> pendingShipmentWaybillsModel, IMapper mapper)
        {
            _pendingShipmentWaybillsModel = pendingShipmentWaybillsModel;
            this.mapper = mapper;
        }

        public  Task<ApiResult<int>> Handle(UpdatePendingShipmentWaybillsCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();
            var model= _pendingShipmentWaybillsModel.GetAll().FirstOrDefault(a=>a.Id==request.Id);
             model = mapper.Map(request, model);
             _pendingShipmentWaybillsModel.Update(model);
            res.Code=ApiEnums.Success;
            res.Msg = "成功";
            res.Data = 1;
            return  Task.FromResult(res);
        }
    }
}
